const indexOptions = Object.freeze([
  { value: 1, lable: '关键业绩考核（KPI）', alias: 'KPI' },
  { value: 2, lable: '能力素质考核（BPI）', alias: 'BPI' },
  { value: 3, lable: '工作计划考核（PPI）', alias: 'PPI' },
  { value: 4, lable: '目标与关键成果法（OKR）', alias: 'OKR' },
  { value: 5, lable: '加减分项', alias: '加减分项' },
  { value: 6, lable: '否决指标法（0-1）', alias: '否决指标法' }
]);
/**
 * 获取维度名称
 * @param type
 * @param nameType 默认0，获取工具名称，1获取工具英文简称
 * @returns
 */
export function getProbationToolName(type: number, nameType?: number) {
  if (!type) return;
  const toolItem = indexOptions.find((item) => item.value === type);
  if (nameType) return toolItem?.alias;
  return toolItem?.lable;
}
// 指标来源
const indexSource = [
  { value: 1, lable: '财务类' },
  { value: 2, lable: '客户类' },
  { value: 3, lable: '内部运营类' },
  { value: 4, lable: '学习与成长类' }
];
/**
 * 获取指标来源名称
 * @param type
 * @returns
 */
export function getIndexSourceName(type: number) {
  const source = indexSource.find((item) => item.value === type);
  return source?.lable;
}
/**
 * 平均分配评分人权重
 * @param scorerList
 * @returns
 */
const allocationWeight = (scorerList: any[]) => {
  // 统计各评分人数、商、余数，是否加入余数
  const statistics = {
    appoint: { count: 0, quotient: 0, remainder: 0, flag: false },
    junior: { count: 0, quotient: 0, remainder: 0, flag: false },
    other: { count: 0, quotient: 0, remainder: 0, flag: false },
    peer: { count: 0, quotient: 0, remainder: 0, flag: false },
    superiors: { count: 0, quotient: 0, remainder: 0, flag: false }
  };
  if (!scorerList.length) return scorerList;
  let scorerCount = scorerList?.length;
  // 是否存在自评人
  const index = scorerList.findIndex((item) => item.type === 5);
  if (index >= 0) scorerCount--;
  if (!scorerCount) return scorerList;
  // 统计人数
  statistics.appoint.count = scorerList.filter(
    (item) => item.type === 1
  ).length;
  statistics.junior.count = scorerList.filter((item) => item.type === 4).length;
  statistics.other.count = scorerList.filter((item) => item.type === 6).length;
  statistics.peer.count = scorerList.filter((item) => item.type === 3).length;
  statistics.superiors.count = scorerList.filter(
    (item) => item.type === 2
  ).length;
  // 统计商
  statistics.appoint.quotient = Math.floor(100 / statistics.appoint.count);
  statistics.junior.quotient = Math.floor(100 / statistics.junior.count);
  statistics.other.quotient = Math.floor(100 / statistics.other.count);
  statistics.superiors.quotient = Math.floor(100 / statistics.superiors.count);
  statistics.peer.quotient = Math.floor(100 / statistics.peer.count);
  // 统计余数
  statistics.appoint.remainder = 100 % statistics.appoint.count;
  statistics.junior.remainder = 100 % statistics.junior.count;
  statistics.other.remainder = 100 % statistics.other.count;
  statistics.superiors.remainder = 100 % statistics.superiors.count;
  statistics.peer.remainder = 100 % statistics.peer.count;
  scorerList.forEach((item, index) => {
    // 自评人不参与分配
    if (item.type === 1) {
      const { quotient, remainder, flag } = statistics.appoint;
      item.weight = flag ? quotient : quotient + remainder;
      statistics.appoint.flag = true;
    }
    if (item.type === 2) {
      const { quotient, remainder, flag } = statistics.superiors;
      item.weight = flag ? quotient : quotient + remainder;
      statistics.superiors.flag = true;
    }
    if (item.type === 3) {
      const { quotient, remainder, flag } = statistics.peer;
      item.weight = flag ? quotient : quotient + remainder;
      statistics.peer.flag = true;
    }
    if (item.type === 4) {
      const { quotient, remainder, flag } = statistics.junior;
      item.weight = flag ? quotient : quotient + remainder;
      statistics.junior.flag = true;
    }
    if (item.type === 6) {
      const { quotient, remainder, flag } = statistics.other;
      item.weight = flag ? quotient : quotient + remainder;
      statistics.other.flag = true;
    }
  });

  return scorerList;
};
export default allocationWeight;
